
function get_arr(t,a) {
    reg = {
        "chunk": [],
        "reg": [1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214],
        "size": 0,
        "write": function (e) {
            var o = typeof e === "string" ? function (e) {
                var n = encodeURIComponent(e).replace(/%([0-9A-F]{2})/g, function (e, t) {
                        return String.fromCharCode("0x" + t);
                    }),
                    o = new Array(n.length);
                return Array.prototype.forEach.call(n, function (e, t) {
                    o[t] = e.charCodeAt(0);
                }), o;
            }(e) : e;
            this.size += o.length;
            var i = 64 - this.chunk.length;
            if (o.length < i) this.chunk = this.chunk.concat(o); else for (this.chunk = this.chunk.concat(o.slice(0, i)); this.chunk.length >= 64;) this._compress(this.chunk), i < o.length ? this.chunk = o.slice(i, Math.min(i + 64, o.length)) : this.chunk = [], i += 64;
        },
        "sum": function (r, n) {
            r && (this.reset(), this.write(r)), this._fill();
            for (var a = 0; a < this.chunk.length; a += 64) this._compress(this.chunk.slice(a, a + 64));
            var c = null;
            if (n == "hex") {
                c = "";
                for (a = 0; a < 8; a++) c += gt(this.reg[a].toString(16), 8, "0");
            } else for (c = new Array(32), a = 0; a < 8; a++) {
                var f = this.reg[a];
                c[4 * a + 3] = (255 & f) >>> 0, f >>>= 8, c[4 * a + 2] = (255 & f) >>> 0, f >>>= 8, c[4 * a + 1] = (255 & f) >>> 0, f >>>= 8, c[4 * a] = (255 & f) >>> 0;
            }
            return this.reset(), c;
        },
        "_compress": function (r) {
            if (r < 64) console.error("compress error: not enough data"); else {
                for (var o = function (e) {
                    for (var t = new Array(132), r = 0; r < 16; r++) t[r] = e[4 * r] << 24, t[r] |= e[4 * r + 1] << 16, t[r] |= e[4 * r + 2] << 8, t[r] |= e[4 * r + 3], t[r] >>>= 0;
                    for (var n = 16; n < 68; n++) {
                        var o = t[n - 16] ^ t[n - 9] ^ Ct(t[n - 3], 15);
                        o = o ^ Ct(o, 15) ^ Ct(o, 23), t[n] = (o ^ Ct(t[n - 13], 7) ^ t[n - 6]) >>> 0;
                    }
                    for (n = 0; n < 64; n++) t[n + 68] = (t[n] ^ t[n + 4]) >>> 0;
                    return t;
                }(r), i = this.reg.slice(0), a = 0; a < 64; a++) {
                    var c = Ct(i[0], 12) + i[4] + Ct(St(a), a),
                        f = ((c = Ct(c = (4294967295 & c) >>> 0, 7)) ^ Ct(i[0], 12)) >>> 0,
                        u = kt(a, i[0], i[1], i[2]);
                    u = (4294967295 & (u = u + i[3] + f + o[a + 68])) >>> 0;
                    var s = xt(a, i[4], i[5], i[6]);
                    s = (4294967295 & (s = s + i[7] + c + o[a])) >>> 0, i[3] = i[2], i[2] = Ct(i[1], 9), i[1] = i[0], i[0] = u, i[7] = i[6], i[6] = Ct(i[5], 19), i[5] = i[4], i[4] = (s ^ Ct(s, 9) ^ Ct(s, 17)) >>> 0;
                }
                for (var l = 0; l < 8; l++) this.reg[l] = (this.reg[l] ^ i[l]) >>> 0;
            }
        },
        "_fill": function () {
            var o = 8 * this.size,
                i = this.chunk.push(128) % 64;
            for (64 - i < 8 && (i -= 64); i < 56; i++) this.chunk.push(0);
            for (var a = 0; a < 4; a++) {
                var c = Math.floor(o / 4294967296);
                this.chunk.push(c >>> 8 * (3 - a) & 255);
            }
            for (a = 0; a < 4; a++) this.chunk.push(o >>> 8 * (3 - a) & 255);
        }
    }

    reg.write(t)
    reg._fill();

    function Ct(e, t) {
        return (e << (t %= 32) | e >>> 32 - t) >>> 0;
    }

    function St(e) {
        return 0 <= e && e < 16 ? 2043430169 : 16 <= e && e < 64 ? 2055708042 : void console.error("invalid j for constant Tj");
    }

    function kt(e, t, r, n) {
        return 0 <= e && e < 16 ? (t ^ r ^ n) >>> 0 : 16 <= e && e < 64 ? (t & r | t & n | r & n) >>> 0 : (console.error("invalid j for bool function FF"), 0);
    }

    function xt(e, t, r, n) {
        return 0 <= e && e < 16 ? (t ^ r ^ n) >>> 0 : 16 <= e && e < 64 ? (t & r | ~t & n) >>> 0 : (console.error("invalid j for bool function GG"), 0);
    }

    for (var i = 0; i < reg["chunk"]["length"]; i += 64) {
        reg["_compress"](reg["chunk"]["slice"](i, i + 64));
    }

    var o = null;
    if (a == "hex") {
        o = "";
        for (i = 0; i < 8; i++)
            o += oe(reg["reg"][i]["toString"](16), 8, "0")
    } else {
        for (o = new Array(32),
                 i = 0; i < 8; i++) {
            var c = reg.reg[i];
            o[4 * i + 3] = (255 & c) >>> 0,
                c >>>= 8,
                o[4 * i + 2] = (255 & c) >>> 0,
                c >>>= 8,
                o[4 * i + 1] = (255 & c) >>> 0,
                c >>>= 8,
                o[4 * i] = (255 & c) >>> 0
        }
    }
    return o;
}

function getABogus(url,data,userAgent){
   // 生成乱码字符串
    var params = url.slice(url.indexOf("?") + 1) + "cus";
	data += "cus";
    var garbledString = getGarbledString(params,data,userAgent);
    var short_str = "Dkdpgh2ZmsQB80/MfvV36XI1R45-WUAlEixNLwoqYTOPuzKFjJnry79HbGcaStCe"
    var ABogus = "";
    // 依次生成七组字符串
    let j = 0
    for (var i = 0; i <= garbledString.length;i += 3) {
        if (i + 3 < garbledString.length) {
            var charCodeAtNum0 = garbledString.charCodeAt(i);
            var charCodeAtNum1 = garbledString.charCodeAt(i + 1);
            var charCodeAtNum2 = garbledString.charCodeAt(i + 2);
            var baseNum = charCodeAtNum2 | charCodeAtNum1 << 8 | charCodeAtNum0 << 16;

            var str1 = short_str[(baseNum & 16515072) >> 18];
            var str2 = short_str[(baseNum & 258048) >> 12];
            var str3 = short_str[(baseNum & 4032) >> 6];
            var str4 = short_str[baseNum & 63];
            ABogus += str1 + str2 + str3 + str4;
        }
        if (i + 3 > garbledString.length){
            let u = garbledString.length - j
            if (u === 2){
                var charCodeAtNum0 = garbledString.charCodeAt(j);
                var charCodeAtNum1 = garbledString.charCodeAt(j + 1);
                var baseNum = charCodeAtNum1 << 8 | charCodeAtNum0 << 16;
                var str1 = short_str[(baseNum & 16515072) >> 18];
                var str2 = short_str[(baseNum & 258048) >> 12];
                var str3 = short_str[(baseNum & 4032) >> 6];
                ABogus += str1 + str2 + str3 + '=';
            }
            if (u === 1){
                var charCodeAtNum0 = garbledString.charCodeAt(j);
                var baseNum = 0 | charCodeAtNum0 << 16;
                var str1 = short_str[(baseNum & 16515072) >> 18];
                var str2 = short_str[(baseNum & 258048) >> 12];
                ABogus += str1 + str2 + '=' + '=';
            }

        }
        j +=3
    }
    return ABogus;
}


function getGarbledString(params,data,userAgent) {
    let timestamp1 = Date.now(),timestamp2 = timestamp1 - Math.floor(Math.random() * 1000);
    let arr_29 = get_arr29(timestamp1,timestamp2,params,data,userAgent)
    let a = randomGarbledCharactersArrayList();
    let b = abGarbledCharacters(String.fromCharCode.apply(null,arr_29))
    return a + b;
}

function abGarbledCharacters(userAgent) {
    let arr_256 = ab_arr_256();
    let n4 = 0;
    let ans = "";
    for (let i = 0; i < userAgent.length; i++) {
        let n2 = i + 1;
        let n3 = n4 + arr_256[n2]
        n4 = n3 % 256
        let old_arr_n2 = arr_256[n2]
        arr_256[n2] = arr_256[n4]
        arr_256[n4] = old_arr_n2
        let n5 = userAgent.charCodeAt(i);
        let n6 = arr_256[n2] + old_arr_n2;
        let n7 = n6 % 256
        let n8 = n5 ^ arr_256[n7]
        ans += String.fromCharCode(n8)
    }
    return ans;
}

function ab_arr_256() {
    let nums = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]
    let previousValue = 0;
    let lm = String.fromCharCode(121);
    for (let i = 0; i < nums.length; i++) {
        previousValue = (previousValue+nums[i]+lm.charCodeAt(0)) % 256
        let tmp = nums[i];
        nums[i] = nums[previousValue]
        nums[previousValue] = tmp
    }
    return nums;
}

function get_arr29(dateTime1,dateTime2,params,data,userAgent) {
    let parArr = get_arr(get_arr(params))
    let dataArr = get_arr(get_arr(data))
    let ua_salt = userAgent.includes('Win') ? 12 : 12;
    var browser_arr = get_arr(encryptionUa(uaGarbledCharacters(userAgent,ua_salt)));
    let num = [50,48,53,54,124,49,48,56,55,124,50,48,53,54,124,49,50,48,56,124,48,124,52,52,124,48,124,48,124,50,48,53,54,124,49,50,48,57,124,50,48,53,54,124,49,51,50,57,124,50,48,53,54,124,49,48,56,55,124,51,48,124,51,48,124,77,97,99,73,110,116,101,108];
    let arr2 = [44, dateTime1 >> 24 & 255, 0, 0, 0, 0 & 255, 10, parArr[21], dataArr[21], 0, browser_arr[23], dateTime1 >> 16 & 255, 0, 0, 0, 1, 0 & 255, 71, parArr[22], dataArr[22], browser_arr[24], dateTime1 >> 8 & 255, 0, 0, 0, 0 & 255, dateTime1 >> 0 & 255, 0, 0, ua_salt & 255, dateTime2 >> 24 & 255, dateTime2 >> 16 & 255, 0, dateTime2 >> 8 & 255, dateTime2 >> 0 & 255, 3, 397, 1, 397, 1, num.length, 0, 0, 0];
    let newArr = [...arr2,...num]
    newArr.push(44^(dateTime1>>24&255)^parArr[21]^dataArr[21]^browser_arr[23]^(dateTime1>>16&255)^1^parArr[22]^dataArr[22]^browser_arr[24]^(dateTime1>>8&255)^(dateTime1>>0&255)^ua_salt^(dateTime2>>24&255)^(dateTime2>>16&255)^(dateTime2>>8&255)^(dateTime2>>0&255)^3^397^1^397^1^71^10^num.length)
    return newArr;
}
function randomGarbledCharactersArrayList() {
     function randomGarbledCharactersArray1() {
        let arr = []
        random = Math.random() * 10000
        num1 = random & 255
        num2 = random >> 8 & 255
        arr.push((num1 & 170) | (3 & 85))
        arr.push((num1 & 85) | (3 & 170))
        arr.push((num2 & 170) | (45 & 85))
        arr.push((num2 & 85) | (45 & 170))
        return String.fromCharCode.apply(null,arr);
    }
    function randomGarbledCharactersArray2() {
        let arr = []
        random = Math.random() * 10000
        num1 = random & 255
        num2 = random >> 8 & 255
        arr.push((num1 & 170) | (1 & 85))
        arr.push((num1 & 85) | (1 & 170))
        arr.push((num2 & 170) | (0 & 85))
        arr.push((num2 & 85) | (0 & 170))
        return String.fromCharCode.apply(null,arr);
    }

    function randomGarbledCharactersArray3() {
        let arr = []
        random = Math.random() * 10000
        num1 = random & 255
        num2 = random >> 8 & 255
        arr.push((num1 & 170) | (1 & 85))
        arr.push((num1 & 85) | (1 & 170))
        arr.push((num2 & 170) | (1 & 85))
        arr.push((num2 & 85) | (1 & 170))
        return String.fromCharCode.apply(null,arr);
    }
    return randomGarbledCharactersArray1()+randomGarbledCharactersArray2()+randomGarbledCharactersArray3()
}

function encryptionUa(ss) {
    let str = "ckdp1h4ZKsUB80/Mfvw36XIgR25+WQAlEi7NLboqYTOPuzmFjJnryx9HVGDaStCe";
    let uaEncryption = "";
    for (let i = 0; i < ss.length; i+=3) {
        let number = (((ss.charCodeAt(i) & 255) << 16) | ((ss.charCodeAt(i+1) & 255) << 8)) | ((ss.charCodeAt(i+2) & 255) << 0);
        uaEncryption += str.charAt((number & 16515072) >> 18);
        uaEncryption += str.charAt((number & 258048) >> 12);
        uaEncryption += str.charAt((number & 4032) >> 6);
        uaEncryption += str.charAt((number & 63) >> 0);
    }
    return uaEncryption
}

function uaGarbledCharacters(userAgent,ua_salt) {
    let arr_256 = ua_arr_256(userAgent,ua_salt);
    let n4 = 0;
    let ans = "";
    for (let i = 0; i < userAgent.length; i++) {
        let n2 = i + 1;
        let n3 = n4 + arr_256[n2]
        n4 = n3 % 256
        let old_arr_n2 = arr_256[n2]
        arr_256[n2] = arr_256[n4]
        arr_256[n4] = old_arr_n2
        let n5 = userAgent.charCodeAt(i);
        let n6 = arr_256[n2] + old_arr_n2;
        let n7 = n6 % 256
        let n8 = n5 ^ arr_256[n7]
        ans += String.fromCharCode(n8)
    }
    return ans;
}


function ua_arr_256(userAgent,ua_salt) {
    let nums = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]
    let previousValue = 0;
    let lm = String.fromCharCode(0.00390625,1,ua_salt);
    for (let i = 0; i < nums.length; i++) {
        previousValue = (previousValue+nums[i]+lm.charCodeAt(i % 3)) % 256
        let tmp = nums[i];
        nums[i] = nums[previousValue]
        nums[previousValue] = tmp
    }
    return nums;
}

module.exports = {getABogus}
